<?php

function getHomeSetting($uid)
{
    $back = T("home_setting")->fetch_all_by_uid($uid);
    if (! $back) {
        insertSetting($uid);
        $back = getHomeSetting($uid);
    }
    $back["display_in_profile_card"] = json_decode($back["display_in_profile_card"]);
    if (isset($back["ban_list"])) {
        $back["ban_list"] = json_decode($back["ban_list"]);
    }
    return $back;
}

function getDetialList($uid)
{
    $data = getHomeSetting($uid);
    $detialban = array();
    foreach ($data["ban_list"] as $f) {
        $detialban[$f] = getCardInfo($f);
    }
    return array(
        "data" => $data,
        "detialban" => $detialban
    );
}

function insertSetting($uid, $display_in_profile_card = array(), $ban_list = array(), $allow_leave_message = 1, $allow_commit = 1)
{
    T("home_setting")->insert(array(
        "uid" => $uid,
        "display_in_profile_card" => json_encode($display_in_profile_card),
        "ban_list" => json_encode($ban_list),
        "allow_leave_message" => $allow_leave_message,
        "allow_commit" => $allow_commit
    ), array(
        "uid",
        "allow_leave_message",
        "allow_commit"
    ));
}

function updateSetting($uid, $display_in_profile_card = array(), $ban_list = array(), $allow_leave_message = 1, $allow_commit = 1)
{
    T("home_setting")->update(array(
        "uid" => $uid,
        "display_in_profile_card" => json_encode($display_in_profile_card),
        "ban_list" => json_encode($ban_list),
        "allow_leave_message" => $allow_leave_message,
        "allow_commit" => $allow_commit
    ), "`uid`='$uid'", array(
        "uid",
        "allow_leave_message",
        "allow_commit"
    ));
}

function allowVisit($uid, $targetid)
{
    $setting = getHomeSetting($targetid);
    if ($setting) {
        if (! in_array($uid, $setting["ban_list"]))
            return true;
    }
    if (isSuperVisitor($uid, $targetid)) {
        return true;
    }

    return false;
}

/**
 * 发送动态
 *
 * @param string $subject
 * @param string $content
 * @param array $appendixes
 * @param array $comments
 * @param int $uid
 * @return boolean
 */
function sendActivity($subject, $content, $appendixes = array(), $view_level = 0, $comments = array(), $uid = -1)
{
    if ($uid == - 1) {
        if (! isLogined()) {
            return false;
        }
        $uid = getSessionUid();
    }
    T("home_activity")->insert(array(
        "uid" => $uid,
        "subject" => $subject,
        "content" => $content,
        "appendixes" => json_encode($appendixes),
        "comments" => json_encode($comments),
        "view_level" => $view_level
    ), array(
        "uid",
        "view_level"
    ));
    return T("home_activity")->getUniqueID();
}

function getActivity($aid)
{
    $back = T("home_activity")->fetch_by_aid($aid);

    if (! $back) {
        return;
    }
    if (isset($back["appendixes"])) {
        $back["appendixes"] = json_decode($back["appendixes"]);
    }
    if (isset($back["comments"])) {
        $back["comments"] = json_decode($back["comments"]);
    }
    return $back;
}

function allowComment($uid)
{
    return getHomeSetting($uid)["allow_commit"];
}

/**
 * uid是否对targetid有管理权限
 *
 * @param int $uid
 * @param int $targetid
 * @return boolean
 */
function isAdmin($uid, $targetid)
{
    if ($uid == $targetid) {
        return true;
    }
    $l_profile = getProfile($uid);
    if ($l_profile["user_type"] == 0) {
        return false;
    }

    if (isSuperVisitor($uid, $targetid)) {
        return true;
    }
    return false;
}

function sendComment($content, $aid, $uid = -1, $parent_cid = -1, $target_cid = -1)
{
    if ($uid = - 1) {
        if (! isLogined()) {
            return false;
        }
        $uid = getSessionUid();
    }
    T("home_activity_comment")->insert(array(
        "uid" => $uid,
        "parent_cid" => $parent_cid,
        "target_cid" => $target_cid,
        "content" => $content,
        "aid" => $aid
    ), array(
        "uid",
        "target_cid",
        "parent_cid",
        "aid"
    ));
    return T("home_activity")->getUniqueID();
}

function getCommentAmount($aid)
{
    return T("home_activity_comment")->count_by_aid($aid);
}

function getCommentTree($aid)
{
    $roots = T("home_activity_comment")->fetch_root_by_aid($aid);
    if (! $roots)
        return array();
    $tree = array();
    foreach ($roots as $v) {
        $v = addCommentProfile($v);
        $s_children = getChildComments($v["cid"]);
        $children = array();
        if ($s_children) {
            foreach ($s_children as $v1) {

                $children[$v1["cid"]] = addCommentProfile($v1);
            }
        }
        $v["children"] = $children;
        $tree[$v["cid"]] = $v;
    }
    foreach ($tree as $v3) {
        if (! $v3["children"])
            continue;
        foreach ($v3["children"] as $v25) {
            if ($v25["target_cid"] == - 1)
                continue;
            if (! isset($v3["children"][$v25["target_cid"]]))
                continue;
            $target = $v3["children"][$v25["target_cid"]];
            $uid = $target["uid"];
            $card = getCardInfo($uid);
            $tree[$v3["cid"]]["children"][$v25["cid"]]["replycard"] = $card;
        }
    }
    return $tree;
}

function getCommentData($cid)
{
    return T("home_activity_comment")->fetch_by_cid($cid);
}

function removeComment($cid)
{
    $data = getCommentData($cid);
    if (! $data) {
        return - 1;
    }
    T("home_activity_comment")->remove_by_cid($cid);
    T("home_activity_comment")->remove_by_parent_cid($cid);
    return 0;
}

function addCommentProfile($comment)
{
    $uid = $comment["uid"];
    $card = getCardInfo($uid);
    $comment["authorcard"] = $card;
    return $comment;
}

function getChildComments($parent_cid)
{
    $children = T("home_activity_comment")->fetch_by_parent_cid($parent_cid);
    if (! $children)
        return array();
    return $children;
}

function removeActivity($aid)
{
    T("home_activity")->remove("`aid`='$aid'");
    T("home_activity_comment")->remove("`aid`='$aid'");
}

function listActivities($uid, $offset, $amount, $visitor)
{
    $data = null;
    if (isAdmin($visitor, $uid)) {
        $data = T("home_activity")->fetch_by_uid($uid, $offset, $amount);
    } else {
        $data = T("home_activity")->fetch_by_uid_public($uid, $offset, $amount);
    }
    if (! $data) {
        return array();
    }
    $result = array();
    foreach ($data as $v) {
        $aid = $v["aid"];
        $commentamount = getCommentAmount($aid);
        if (isset($v["appendixes"])) {
            $v["appendixes"] = json_decode($v["appendixes"]);
        }
        if (isset($v["comments"])) {
            $v["comments"] = json_decode($v["comments"]);
        }
        $v["card"] = getCardInfo($v["uid"]);
        $v["commentsamount"] = $commentamount;
        $v["allowComment"] = $uid == $visitor || allowComment($uid);
        $v["isAdmin"] = isAdmin($visitor, $uid);
        $result[$aid] = $v;
    }
    return $result;
}

function listActivitiesByClassid($classid, $offset, $amount, $visitor)
{
    $data = T("manager_linked")->fetch_activity_by_classid($classid, $offset, $amount);
    if (! $data) {
        return array();
    }
    $result = array();
    foreach ($data as $v) {
        $aid = $v["aid"];
        $commentamount = getCommentAmount($aid);
        if (isset($v["appendixes"])) {
            $v["appendixes"] = json_decode($v["appendixes"]);
        }
        if (isset($v["comments"])) {
            $v["comments"] = json_decode($v["comments"]);
        }
        $v["card"] = getCardInfo($v["uid"]);
        $v["commentsamount"] = $commentamount;
        $v["allowComment"] = $v["uid"] == $visitor || allowComment($v["uid"]);
        $v["isAdmin"] = isAdmin($visitor, $v["uid"]);
        $result[$aid] = $v;
    }
    return $result;
}

function getLeaveMessageBlockList($uid)
{
    $data = T("home_leavemessage_block_list")->fetch_by_uid($uid);
    if (! $data) {
        return array();
    }
    return json_decode($data["blocklist"]);
}

function isLeaveMessageBlocked($uid, $target)
{
    return in_array($target, getLeaveMessageBlockList($uid));
}

function insertLMBlock($uid)
{
    T("home_leavemessage_block_list")->insert(array(
        "uid" => $uid,
        "blocklist" => json_encode(array())
    ), array(
        "uid"
    ));
}

function addLeaveMessageBlock($uid, $targetid)
{
    $data = T("home_leavemessage_block_list")->fetch_by_uid($uid);
    if (! $data) {
        insertLMBlock($uid);
        $data = array();
    }
    if (! in_array($targetid, $data)) {
        $data[] = $targetid;
    }
    T("home_leavemessage_block_list")->update(array(
        "blocklist" => json_encode($data)
    ), "`uid`=$uid");
}

function removeLeaveMessageBlock($uid, $targetid)
{
    $data = T("home_leavemessage_block_list")->fetch_by_uid($uid);
    if (! $data) {
        return;
    }
    if (! in_array($targetid, $data)) {
        return;
    }
    $data = array_diff($data, array(
        $targetid
    ));
    T("home_leavamessage_block_list")->update(array(
        "blocklist" => json_encode($data)
    ), "`uid`=$uid");
}

function allowLeaveMessage($uid, $targetid)
{
    if (isAdmin($uid, $targetid))
        return true;
    $homeSetting = getHomeSetting($targetid);

    if (! $homeSetting["allow_leave_message"]) {
        return false;
    }
    if (isLeaveMessageBlocked($uid, $targetid)) {
        return false;
    }
    return true;
}

function sendLeaveMessage($uid, $content, $target, $quote = -1)
{
    T("home_leavemessage")->insert(array(
        "uid" => $uid,
        "content" => $content,
        "target" => $target,
        "quote" => $quote
    ));
    return T("home_leavemessage")->getUniqueID();
}

function getLeaveMessage($lid)
{
    return T("home_leavemessage")->fetch_by_lid($lid);
}

function setLeaveMessageReaded($lid)
{
    return T("home_leavemessage")->update_read_state($lid);
}

function listLeaveMessageByUID($uid, $offset, $amount)
{
    $data = T("home_leavemessage")->fetch_by_uid($uid, $offset, $amount);
    if (! $data) {
        return array();
    }
    return addLeaveMessageCard($data);
}

function listLeaveMessageByTargetID($targetid, $offset, $amount)
{
    $data = T("home_leavemessage")->fetch_by_target($targetid, $offset, $amount);
    if (! $data) {
        return array();
    }
    return addLeaveMessageCard($data);
}

function getNotReadLM($targetid)
{
    $data = T("home_leavemessage")->fetch_not_read($targetid);
    if (! $data) {
        return array();
    }
    return addLeaveMessageCard($data);
}

function listLeaveMessageByUIDToTargetID($uid, $targetid, $offset, $amount)
{
    $data = T("home_leavemessage")->fetch_by_uid_and_target($uid, $targetid, $offset, $amount);
    if (! $data) {
        return array();
    }
    return addLeaveMessageCard($data);
}

function addLeaveMessageCard($data)
{
    $back = array();
    foreach ($data as $v) {
        $v["ucard"] = getCardInfo($v["uid"]);
        $v["tcard"] = getCardInfo($v["target"]);
        if ($v["quote"] != - 1) {
            $v["content"] = base64_encode(getQuote($v["quote"]) . $v["content"]);
        } else {
            $v["content"] = base64_encode($v["content"]);
        }
        $back[$v["lid"]] = $v;
    }
    return $back;
}

function getQuote($quote, $isData = false)
{
    $q = null;
    if (! $isData) {
        $q = getLeaveMessage($quote);
    } else {
        $q = $quote;
    }
    if (! $q) {
        return false;
    }
    $uid = $q["uid"];
    $profile = getProfile($uid);
    if (! $profile) {
        return false;
    }
    $name = $profile["name"];
    $content = $q["content"];
    if (mb_strlen($content) > 20) {
        $content = mb_strcut($content, 0, 40) . "...";
    }
    return <<<END
        <div class="s_quote">回复：<a href="index.php?mod=home&childmod=zone&uid=$uid" class="s_quote">$name</a> $content </div><br>
    END;
}

function getLeaveMessageNotReadAmount($uid)
{}
?>